using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class ExcavatorBoosterController : SingletonMB<ExcavatorBoosterController>
{
	public List<BoostButtonSettings> ButtonSettings = new List<BoostButtonSettings>();

	public MissileLauncher Launcher;

	public GameObject BoostButton;

	public GameObject VideoImage;

	public TextMeshProUGUI BoostDescription;

	public Image icon;

	private BoostButtonType CurrentBoost;

	[Header("Eco Mode")]
	public DOTweenAnimation _solarPanel;

	private static bool FirstUse
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("First_Time_RV_Boost", 0) == 0;
		}
		set
		{
			NintendoPlayerPrefs.SetInt("First_Time_RV_Boost", (!value) ? 1 : 0);
		}
	}

	private void Start()
	{
		BoostButton.SetActive(value: false);
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStart));
		GameManager instance2 = SingletonMB<GameManager>.Instance;
		instance2.OnGameEnd = (Action)Delegate.Combine(instance2.OnGameEnd, new Action(OnGameEnd));
		_solarPanel.gameObject.SetActive(value: false);
	}

	private void OnGameEnd()
	{
		BoostButton.SetActive(value: false);
		StopAllCoroutines();
		_solarPanel.gameObject.SetActive(value: false);
	}

	private void OnGameStart()
	{
		BoostButton.SetActive(value: false);
		if (Preloader.ABTestConfig.RVBoosters && SingletonMB<GameManager>.Instance.ReturnCount > 5 && SingletonMB<GameManager>.Instance.ReturnCount % 2 == 0)
		{
			StartCoroutine(CheckForButton());
		}
		if (SingletonMB<GameManager>.Instance.EcoMode)
		{
			_solarPanel.gameObject.SetActive(value: true);
			StartCoroutine(CheckEco());
		}
		else
		{
			_solarPanel.gameObject.SetActive(value: false);
		}
	}

	private IEnumerator CheckEco()
	{
		while (SingletonMB<GameManager>.Instance.EcoMode)
		{
			yield return null;
		}
		_solarPanel.DOPlayBackwards();
		yield return new WaitForSeconds(2f);
		_solarPanel.gameObject.SetActive(value: false);
	}

	private IEnumerator CheckForButton()
	{
		yield return null;
		VideoImage.SetActive(!FirstUse);
		SetBooster(BoostButtonType.Missile);
		int progress = UnityEngine.Random.Range(20, 40);
		yield return new WaitWhile(() => SingletonMB<Progress>.Instance.Pr() < progress);
		BoostButton.SetActive(value: true);
		float value = UnityEngine.Random.value;
		if (value <= 1f / 3f)
		{
			SetBooster(BoostButtonType.Multiplier);
		}
		else if (value <= 2f / 3f)
		{
			SetBooster(BoostButtonType.ExtraOil);
		}
		else
		{
			SetBooster(BoostButtonType.Missile);
		}
	}

	private void SetBooster(BoostButtonType current)
	{
		CurrentBoost = current;
		foreach (BoostButtonSettings buttonSetting in ButtonSettings)
		{
			if (buttonSetting.ButtonType == current)
			{
				BoostDescription.text = buttonSetting.ButtonText;
				icon.sprite = buttonSetting.sprite;
				icon.color = buttonSetting.color;
			}
		}
	}

	public void OnClick()
	{
		BoostButton.SetActive(value: false);
		StopAllCoroutines();
		if (FirstUse)
		{
			AwardBooster();
			FirstUse = false;
		}
	}

	private void AwardBooster()
	{
		switch (CurrentBoost)
		{
		case BoostButtonType.Missile:
			Launcher.gameObject.SetActive(value: true);
			break;
		case BoostButtonType.ExtraOil:
			SingletonMB<GameManager>.Instance.currentOilUI.AddOil(60);
			break;
		case BoostButtonType.Multiplier:
			UnityEngine.Object.FindObjectOfType<Sell>().AugmentMultiplier(3);
			break;
		}
	}
}
